package com.studentManagement.utils;


import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Administrator
 * @date 2017/5/18 0018
 */
public class  Bean_MapUtil {
    //javabean类转map
    public static Map<String,Object> transform(Object javabean) {
        if (javabean==null){
            return null;
        }
        Map<String,Object> map=new HashMap<String,Object>();
        try {
            BeanInfo beanInfo= Introspector.getBeanInfo(javabean.getClass());
            PropertyDescriptor[] propertyDescriptors=beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property:propertyDescriptors){
                String key=property.getName();
                if (!key.equals("class")){
                    Method getter=property.getReadMethod();
                    Object value=getter.invoke(javabean);
                    map.put(key,value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  map;
    }
    public static void main(String[]args){

    }
}
